package com.jxt.myjxtmanager.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;

import com.jxt.myjxtmanager.BaseActivity;
import com.jxt.myjxtmanager.MainActivity;
import com.jxt.myjxtmanager.R;
import com.jxt.myjxtmanager.db.SaleUser;
import com.jxt.myjxtmanager.db.SaleUserModel;
import com.jxt.myjxtmanager.utils.SharePreferenceUtils.SharedPrefUtil;

import cn.jpush.sms.SMSSDK;

/**
 * Created by qm on 2017/2/7 0007.
 */
public class StartActivity extends BaseActivity {

    private View view;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_start;
    }

    @Override
    protected void initView() {
//		this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
        view = getView(R.id.iv_start_view);
        // 执行动画
        animation();
        SMSSDK.getInstance().initSdk(this);
        SMSSDK.getInstance().setIntervalTime(30 * 1000);

    }

    private void animation() {
        AnimationSet animationSet = new AnimationSet(true);
        AlphaAnimation alphaAnimation = new AlphaAnimation(0.2F, 0.9F);
        alphaAnimation.setDuration(1000);
        animationSet.addAnimation(alphaAnimation);
        animationSet.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

                skip();
            }
        });
        view.startAnimation(animationSet);
    }

    private void skip() {
        SharedPrefUtil shred = SharedPrefUtil.getInstance(this);
        boolean single = shred.getBoolean("singleCompany", false);
//        boolean single = true;
        if (single) {
            toStart(shred);
        } else {
            shred.putBoolean("singleCompany", true);
            shred.commit();
            toStart(shred);
            /*
            SkipActivityFinish(GuideActivity.getIntent(this));*/
        }

    }


    /**
     * 获取 设备IMEI
     *
     * @param context
     * @return
     */
    public static String getIMEI(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        return tm.getDeviceId();
    }

    public void toStart(SharedPrefUtil shred) {
        SaleUser saleUser = SaleUserModel.getInstance().loadCurrentSaleUsers();
        if (saleUser != null) {
            long sale_id = saleUser.getUser_id() == null ? 0 : saleUser.getUser_id();
            if (sale_id <= 0 || TextUtils.isEmpty(saleUser.getPassword()) || TextUtils.isEmpty(saleUser.getToken())) {//如果未登录则去登陆
                SkipActivityFinish(LoginActivity.getIntent(this));
                return;
            }
            SkipActivityFinish(new Intent(StartActivity.this, MainActivity.class));
        }else{
            SkipActivityFinish(LoginActivity.getIntent(this));
        }
    }
}
